package org.proxycine.gestsalles.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.proxycine.gestsalles.model.entities.Employe;
import org.proxycine.gestsalles.model.entities.RoleEmploye;

public class EmployeDao extends Adapter {
	
private static EmployeDao instance;
	
	private EmployeDao() {
		instance = this;
	}
	
	public static EmployeDao getInstance() {
		if(instance == null)
			instance = new EmployeDao();
		
		return instance;
	}
	
	public Employe getEmployeFromId(int id) {
		Employe employe = null;
		
		String request = "SELECT * " +
				"FROM Employe E " +
				"WHERE E.id = '" + id + "'";
				
				try {
					ResultSet rs = getStatement().executeQuery(request);
					
					if (rs.next()) {
						employe = new Employe(rs.getInt("id"),
								rs.getString("prenom"),
								rs.getString("nom"),
								rs.getString("login"),
								rs.getString("password"),
								RoleEmploye.valueOf(rs.getString("role")));
					}
					
					rs.close();
					
				} catch (SQLException e) {
					e.printStackTrace();
				}			
		return employe;
	}
	
	public Employe getEmployeFromLogin(String login) {
		Employe employe = null;
		
		String request = "SELECT * " +
				"FROM Employe E " +
				"WHERE E.login = '" + login + "'";
				
				try {
					ResultSet rs = getStatement().executeQuery(request);
					
					if (rs.next()) {
						employe = new Employe(rs.getInt("id"),
								rs.getString("prenom"),
								rs.getString("nom"),
								rs.getString("login"),
								rs.getString("password"),
								RoleEmploye.valueOf(rs.getString("role")));
					}
					
					rs.close();
					
				} catch (SQLException e) {
					e.printStackTrace();
				}			
		return employe;
	}
	
}
